/*
 * Copyright (c) 2023 Huawei Technologies Co., Ltd.
 * openUBMC is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *         http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

#include "interface/network_adapter.h"
#include <functional>

namespace dev {

bool NetworkAdapter::update_firmware_version_by_pldm() {
    emulex_fru_info_t fruinfo = {};
    bool ret = m_pldm_over_mctp_emulex->get_emulex_fru_info(&fruinfo);
    if (!ret) {
        ilog("Error: Failed to update firmware_version");
        return false;
    }
    FirmwareVersion.set_value(fruinfo.firmware_version);
    return true;
}

void NetworkAdapter::start_pldm_update_task_emulex(pldm_over_mctp_emulex_ptr pldm_over_mctp,
                                                   mc::milliseconds      interval) {
    m_pldm_timer            = new mc::core::timer(this);
    m_pldm_over_mctp_emulex = pldm_over_mctp;
    m_pldm_timer->timeout.connect([this]() {
        update_firmware_version_by_pldm();
    });
    m_pldm_timer->set_single_shot(false);
    m_pldm_timer->start(interval);
}

void NetworkAdapter::stop_pldm_update_task() {
    if (m_pldm_timer) {
        m_pldm_timer->stop();
    }
    if (m_pldm_over_mctp_emulex) {
        m_pldm_over_mctp_emulex = nullptr;
    }
}

void NetworkAdapter::register_os_reset_callback(const os_reset_callback& callback) {
    m_os_reset_callback = callback;
}

void NetworkAdapter::unregister_os_reset_callback() {
    m_os_reset_callback = nullptr;
}

void NetworkAdapter::HandleOsResetSignal() {
    if (m_os_reset_callback) {
        m_os_reset_callback();
    }
}


} // namespace dev

MC_REFLECT(dev::NetworkAdapter)